var dataHeaderUI = '#dataHeader';
var dataBodyUI = '#dataBody';
var cntUI = '#txtTotalBooking';

var message = function(text) {
	var msgUI = '#txtMessage';
	$(msgUI).empty();
	$(msgUI).append(text);
};

var showAvailRoomsOnUI = function(resp) {
	var isDbg = miShare(isDebug);
	$(dataHeaderUI).empty();
	$(dataHeaderUI)
			.append(
					'<tr><th>Location</th><th>Room Id</th><th>Name</th><th>Comments</th></tr>');
	$(dataBodyUI).empty();
	var aRowTag = new EJS({
		url : 'available.html'
	});

	$(dataBodyUI).empty();
	var cnt = 0;
	$.each(resp, function(index, item) {
		if (isDbg) {
			console.log('item:');
			console.log(typeof item);
			console.log(item);
		}
		cnt++;
		var data = {
			location : item.location,
			name : item.name,
			roomId : item.roomId,
			comments : item.comments
		};
		$(dataBodyUI).append(aRowTag.render(data));
	});

	$(cntUI).empty();
	$(cntUI).append(cnt);
};

var listAvail = function() {
	var url = '../rest/booking/rooms/available';
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		message('Available rooms listed.');
		showAvailRoomsOnUI(resp);
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		message('Fail to list available rooms.\nCheck logs for more info.');
	});
	console.log('listAvail().Sent:\n' + JSON.stringify(reqMsg));
};

var showBookedRoomsOnUI = function(resp) {
	$(dataHeaderUI).empty();
	$(dataHeaderUI)
			.append(
					'<tr><th>Booking Id</th><th>Room Id</th><th>Booked By</th><th>Start Time</th><th>End Time</th></tr>');
	$(dataBodyUI).empty();
	var aRowTag = new EJS({
		url : 'booking.html'
	});
	$(dataBodyUI).empty();
	var cnt = 0;
	$.each(resp, function(index, item) {
		cnt++;
		var data = {
			bookingId : item.bookingId,
			roomId : item.roomId,
			bookedBy : item.bookedBy,
			startTime : item.startTime,
			endTime : item.endTime
		};
		$(dataBodyUI).append(aRowTag.render(data));
	});
	$(cntUI).empty();
	$(cntUI).append(cnt);
};

var listBooked = function() {
	var url = '../rest/booking/rooms/booked';
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		message('Booked rooms listed.');
		showBookedRoomsOnUI(resp);
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		message('Fail to list booked rooms.\nCheck logs for more info.');
	});
	console.log('listBooked().Sent:\n' + JSON.stringify(reqMsg));
};

var unreserve = function() {
	var bookingId = $('#txtUnBookingId').val();
	var url = '../rest/booking/rooms/' + bookingId;
	var data = {};
	var reqMsg = deljax(url, data, function(resp) {
		message('Canceled Booking:' + bookingId);
		listBooked();
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		message('Failure to Cancel Booking:' + bookingId
				+ '\nCheck logs for more info.');
	});
	console.log('unreserve().Sent:\n' + JSON.stringify(reqMsg));
};

var reserveJax = function(roomId, bookingId, bookedBy, startTime, endTime,
		successFunc, errorFunc) {
	var url = '../rest/booking/rooms';
	var data = {
		roomId : roomId,
		bookingId : bookingId,
		bookedBy : bookedBy,
		startTime : startTime,
		endTime : endTime
	};
	return posjax(url, data, successFunc, errorFunc);
	// console.log('Using $.post(...)');
	// use $.post(..) for @FormParam does not works!!!
	// $.post(url, data, null, 'application/x-www-form-urlencoded').done(successFunc).fail(errorFunc);
};

var fillBooking = function() {

	reserveJax('khtp002', 'st002', 'sam_tan', '2013-07-21', '2013-07-21');
	reserveJax('khtp003', 'st003', 'sam_tan', '2013-07-22', '2013-07-23');
	reserveJax('tpm001', 'nr001', 'nazri', '2013-07-18', '2013-07-19');
	reserveJax('tpm003', 'am001', 'azmi', '2013-07-20', '2013-07-21');
	listBooked();
};

var reserve = function() {
	var roomId = $('#txtRoomId').val();
	var bookingId = $('#txtBookingId').val();
	var bookedBy = $('#txtBookedBy').val();
	var startTime = $('#txtStartTime').val();
	var endTime = $('#txtEndTime').val();
	var reqMsg = reserveJax(roomId, bookingId, bookedBy, startTime, endTime,
			function(resp) {
				message('Reservation made:' + bookingId);
				listBooked();
			}, function(resp, status) {
				console.log(resp);
				console.log(status);
				message('Failure to make Reservation:' + bookingId
						+ '\nCheck logs for more info.');
			});
	console.log('reserve().Sent:\n' + JSON.stringify(reqMsg));
};